﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Diagnostics;
using Microsoft.TeamFoundation.Build.Client;

namespace TeamDeploy.Activities
{
    [BuildActivity(HostEnvironmentOption.Agent)]
    public sealed class Uninstall : CustomActivityBase
    {

        [RequiredArgument]
        public InArgument<string> ProductCode { get; set; }

        [RequiredArgument]
        public InArgument<string> TargetMachine { get; set; }

        [RequiredArgument]
        public InArgument<string> RemoteExecutePathFilename { get; set; }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            string productCode = context.GetValue(this.ProductCode);
            string targetMachine = context.GetValue(this.TargetMachine);
            string remoteExecutePathFilename = context.GetValue(this.RemoteExecutePathFilename);
            bool ignoreExitCode = false;
            bool.TryParse(context.GetValue(this.IgnoreExitCode), out ignoreExitCode);

            try
            {
                if (!System.IO.File.Exists(remoteExecutePathFilename))
                {
                    throw new ArgumentException("RemoteExecute does not exists at: " + remoteExecutePathFilename);
                }

                string uninstallArgs = "/qn /x";
                string args = @"\\" + targetMachine + " -i 0 -s -accepteula " + "msiexec.exe " + uninstallArgs + " " + productCode;

                using (System.Diagnostics.Process process = new System.Diagnostics.Process())
                {
                    process.StartInfo.FileName = remoteExecutePathFilename;
                    //process.StartInfo.WorkingDirectory = SearchPathRoot.Get(context);                    
                    process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.ErrorDialog = false;
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.Arguments = args;

                    try
                    {
                        process.Start();
                        string output = process.StandardOutput.ReadToEnd();
                        process.WaitForExit();
                        LogMessage(context, "Uninstall Output:" + output);
                    }
                    catch (Exception ex)
                    {
                        LogMessage(context, ex.ToString());
                        throw ex;
                    }


                if (process.ExitCode != 0)
                {
                    string message = "Uninstalling " + productCode + " on " + targetMachine + " returned code:" + process.ExitCode;

                    if (ignoreExitCode)
                    {
                       LogMessage(context, message);
                    }
                    else
                    {
                       throw new Exception(message);
                    }
                }
                }

                //if (BuildEngine != null)
                //{
                //    Log.LogMessage("Uninstalling " + ProductCode + " on " + TargetMachine + " completed successfully.");
                //}
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
